package com.flute.framework.data.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

import com.flute.framework.data.AbstractRawdataParser;
import com.flute.framework.data.Property;
import com.flute.framework.util.XMLUtil;
import com.flute.tools.data.DataIterator;
import com.flute.tools.data.DataRecord;
import com.flute.tools.util.exceptoin.DataParserException;

public class CustomRawParser extends AbstractRawdataParser {

	private final static String SEPERATOR = ",";

	@Override
	public DataIterator<DataRecord> parse(File f) throws DataParserException {
		List<DataRecord> dataRecordList = new ArrayList<DataRecord>();
		String globalPath = getGlobalPath();
		List<Property> props = getProps();

		SAXBuilder sb = new SAXBuilder();
		try {
			Document doc = sb.build(f);
			Element root = doc.getRootElement();
			System.out.println(globalPath);
			List<Element> elementList = XPath.selectNodes(root, globalPath);
			for (Element element : elementList) {
				Document subDoc = XMLUtil.getDocument(element);
				DataRecord dr = new DataRecord();
				for (Property prop : props) {
					StringBuilder value = new StringBuilder("");
					List<Element> subElement = XPath.selectNodes(subDoc, prop
							.getValue());
					for (Element e : subElement) {
						value.append(e.getTextNormalize()).append(SEPERATOR);
					}
					String tmpValue = value.toString();
					if (tmpValue.endsWith(SEPERATOR)) {
						tmpValue = tmpValue.substring(0, tmpValue.length()
								- SEPERATOR.length());
					}
					dr.AddField(prop.getName(), tmpValue);
				}
				dataRecordList.add(dr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RawDataIterator<DataRecord>(dataRecordList.iterator());
	}

	public static void main(String[] args) {

		try {
			CustomRawParser parser = new CustomRawParser();
			parser.setGlobalPath("/songs/song");

			List<Property> props = new ArrayList<Property>();
			parser.setProps(props);
			props.add(new Property("song_id", "//song/song_id"));
			props.add(new Property("colorring_id", "//song/colorring_id"));
			props.add(new Property("song_name", "//song/song_name"));
			props.add(new Property("lyric", "//song/lyric"));
			props.add(new Property("business_count", "//song/business_count"));
			props.add(new Property("istop", "//song/istop"));
			props.add(new Property("action", "//song/action"));
			props.add(new Property("singer_id", "//song/singers/singer/singer_id"));
			props.add(new Property("singer_name", "//song/singers/singer/singer_name"));
			props.add(new Property("album_id", "//song/albums/album/album_id"));
			props.add(new Property("album_name", "//song/albums/album/album_name"));
			props.add(new Property("flag", "//song/flag"));

			DataIterator<DataRecord> it = parser.parse(new File(
					"E:\\安装软件\\data\\song1.xml"));
			while (it.hasNext()) {
				DataRecord dr = it.next();
				String[] fields = dr.getAllFields();
				StringBuilder sb = new StringBuilder();
				for (String field : fields) {
					sb.append(field).append(":")
							.append(dr.getFieldValue(field)).append(" ");
				}
				System.out.println(sb.toString());
			}

		} catch (DataParserException e) {
			e.printStackTrace();
		}
	}
}
